package com.hualude.roadsign.repository;

import com.hualude.roadsign.dto.DTRequestDto;
import com.hualude.roadsign.dto.DTSolarFaultDto;
import com.hualude.roadsign.dto.SolarEnergyFaultDto;
import com.hualude.roadsign.model.SolarFaultEntity;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;

import java.util.List;

/**
 * Created by ding on 2017/7/10.
 */
public interface SolarFaultRepository extends CrudRepository<SolarFaultEntity, Long> {

    /**
     * @Description: 获取太阳能信息datatable
     * @Author: pan-jianan
     * @Date: 2017/7/12 18:45
     */
    public Integer getSolarFaultCount4DT(Integer actorId, Integer userId, Integer orgId, String strSearch) throws Exception;

    public List<DTSolarFaultDto> getSolarFault4DT(DTRequestDto dtRequestDto, Integer actorId, Integer userId, Integer orgId) throws Exception;

    /**
     * @Description: 太阳能故障详情
     * @Author: pan-jianan
     * @Date: 2017/7/12 18:47
     */
    @Query(value = "select new com.hualude.roadsign.dto.SolarEnergyFaultDto(sof.solarCheck,sof.solarMainCheck,sof.solarFeedback,sof.solarTime,sof.solarLevel,sof.solarPhoto,sof.solarType," +
            "sof.solarReason,sof.solarCreatorId,sof.solarMoney) " +
            "from SolarFaultEntity sof where sof.solarFaultId=?1")
    public SolarEnergyFaultDto getSolarFaultInfo2Fault(Integer solarFaultId) throws Exception;

    /**
     * @Description: 故障一级审核
     * @Author: pan-jianan
     * @Date: 2017/7/12 21:39
     */
    @Modifying
    @Query(value = "update SolarFaultEntity sof set sof.solarCheck=?1,sof.solarFeedback=?2,sof.solarCheckerId=?4,sof.solarCheckTime=?5 where sof.solarFaultId=?3")
    public Integer solarFaultAudit(Integer isPass, String solarFeedback, Integer solarFaultId, Integer solarCheckerId, String solarCheckTime) throws Exception;

    /**
     * @Description: 删除故障
     * @Author: pan-jianan
     * @Date: 2017/7/22 21:38
     */
    @Modifying
    @Query(value = "delete from SolarFaultEntity sof where sof.solarFaultId=?1")
    public Integer deleteSolarFaultByIds(Integer solarFaultId) throws Exception;

    /**
     * @Description: 删除太阳能产品故障
     * @Author: shao-yikun
     * @Date: 2017/9/5 下午2:54
     */
    @Modifying
    @Query(value = "delete from SolarFaultEntity where solarEnergyId = ?1")
    public Integer deleteSolarFault(Integer solarEnergyId) throws Exception;

    /**
     * @Description: 太阳能故障审核DT
     * @Author: jiang-weirong
     * @Date: 2017/7/15 13:40
     */
    public List<DTSolarFaultDto> getSolarFault4DT(DTRequestDto dtRequestDto, Integer actorId, Integer orgId) throws Exception;

    public Integer getSolarFaultCount4DT(DTRequestDto dtRequestDto, Integer actorId, Integer orgId) throws Exception;

    /**
     * @Description: 太阳能故障处理DT
     * @Author: jiang-weirong
     * @Date: 2017/7/15 13:41
     */
    public List<DTSolarFaultDto> getSolarHandle4DT(DTRequestDto dtRequestDto, String actorIds, Integer orgId, Integer solarMaintenanceStatus) throws Exception;

    public Integer getSolarHandleCount4DT(DTRequestDto dtRequestDto, String actorIds, Integer orgId, Integer solarMaintenanceStatus) throws Exception;

    /**
     * @Description: 审核通过，SolarMaintenanceStatus：0->1
     * @Author: jiang-weirong
     * @Date: 2017/7/12 16:15
     */
    @Modifying
    @Query(value = "UPDATE SolarFaultEntity  sf SET sf.solarMainCheck = ?1,sf.solarMainCheckerId=?4,sf.solarFeedback=?2,sf.solarMaintenanceStatus=1,sf.solarMainCheckTime=?5 WHERE sf.solarFaultId =?3")
    public Integer solarFaultAudit2Success(Integer isPass, String solarFeedback, Integer solarFaultId, Integer userId, String dateNowStr) throws Exception;

    /**
     * @Description: 审核不通过
     * @Author: jiang-weirong
     * @Date: 2017/7/15 13:41
     */
    @Modifying
    @Query(value = "UPDATE SolarFaultEntity  sf SET sf.solarMainCheck = ?1,sf.solarMainCheckerId=?4,sf.solarFeedback=?2,sf.solarMainCheckTime=?5 WHERE sf.solarFaultId =?3")
    public Integer solarFaultAudit2Fail(Integer isPass, String solarFeedback, Integer solarFaultId, Integer userId, String dateNowStr) throws Exception;

    /**
     * @Description: 施工方案
     * @Author: jiang-weirong
     * @Date: 2017/7/15 13:42
     */
    @Modifying
    @Query(value = "UPDATE SolarFaultEntity  sf SET sf.solarMaintenanceContent = ?2 WHERE sf.solarFaultId =?1")
    public Integer addSolarMaintenanceContent(Integer solarFaultId, String solarMaintenanceContent) throws Exception;

    /**
     * @Description: 完成故障维修
     * @Author: jiang-weirong
     * @Date: 2017/7/15 13:43
     */
    @Modifying
    @Query(value = "UPDATE SolarFaultEntity  sf SET sf.solarMaintenancePhoto = ?1,sf.solarMaintainerId=?2,sf.solarMaintenanceTime=?3,sf.solarMaintenanceExplain=?4,sf.solarMaintenanceStatus=2 WHERE sf.solarFaultId =?5")
    public Integer finishSolarMaintenance(String solarMaintenancePhoto, Integer solarMaintainerId, String solarMaintenanceTime, String solarMaintenanceExplain, Integer solarFaultId) throws Exception;

    public Integer getSolarFaultReportCount4DT(String search, Integer actorId, Integer orgId, String solarEnergyStartRoad, String solarEnergyEndRoad, String solarEnergyRoadName) throws Exception;

    public List<DTSolarFaultDto> getSolarFaultReport4DT(DTRequestDto dtRequestDto, Integer actorId, Integer orgId, String solarEnergyStartRoad, String solarEnergyEndRoad, String solarEnergyRoadName) throws Exception;

    /**
     * @Description: 更新故障
     * @Author: pan-jianan
     * @Date: 2017/7/22 20:57
     */
    @Modifying
    @Query(value = "UPDATE SolarFaultEntity  sf SET sf.solarLevel = ?2,sf.solarType = ?3,sf.solarPhoto = ?4,sf.solarReason = ?5,sf.solarMoney =?6 WHERE sf.solarFaultId =?1")
    public Integer updateSolarFault(Integer solarFaultId, Integer solarLevel, Integer solarType, String solarPhoto, String solarReason, Double solarMoney) throws Exception;

    /**
     * @Description: 获取太阳能审核列表4App
     * @Author: pan-jianan
     * @Date: 2017/7/17 16:18
     */
    public List<SolarEnergyFaultDto> getSolarFaultCheckList4App(Integer orgId, String actorIds, Integer faultStatus,
                                                                Integer count, Integer size) throws Exception;

    /**
     * @Description: 获取一级审核状态
     * @Author: pan-jianan
     * @Date: 2017/7/18 18:57
     */
    @Query(value = "SELECT solarCheck FROM SolarFaultEntity WHERE solarFaultId =?1")
    public Integer getSolarIsCheck(Integer solarFaultId) throws Exception;

    /**
     * @Description: 获取太阳能维修列表4App
     * @Author: pan-jianan
     * @Date: 2017/7/20 14:29
     */
    public List<SolarEnergyFaultDto> getSolarFaultHandleList4App(Integer orgId, Integer maintenanceStatus, String actorIds, Integer count, Integer size) throws Exception;

    public List<SolarEnergyFaultDto> getSolarFaultList4App(Integer orgId, String actorIds, Integer faultStatus, Integer count, Integer size) throws Exception;

    /**
     * @Description: 回执单信息
     * @Author: jiang-weirong
     * @Date: 2017/7/20 16:15
     */
    @Query(value = "select new com.hualude.roadsign.dto.SolarEnergyFaultDto(se.solarEnergyId,sf.solarLevel, sf.solarType," +
            "sf.solarReason,se.solarEnergyLongitude,se.solarEnergyLatitude,se.solarEnergyProvince,se.solarEnergyCity," +
            " se.solarEnergyArea, se.solarEnergyRoadName, se.solarEnergyStartRoad, se.solarEnergyEndRoad, sf.solarMaintenancePhoto," +
            " sf.solarMaintenanceContent, sf.solarMaintenanceTime,sf.solarPhoto,sf.solarMainCheckTime,sf.solarMaintenanceExplain,sf.solarFeedback)" +
            " from SolarEnergyEntity se,SolarFaultEntity sf " +
            "WHERE se.solarEnergyId=sf.solarEnergyId AND sf.solarFaultId = ?1")
    public SolarEnergyFaultDto getReceiptInfo4App(Integer solarFaultId) throws Exception;

    /**
     * @Description: 获取推送相关信息
     * @Author: jiang-weirong
     * @Date: 2017/9/11 10:56
     */
    @Query(value = "select new com.hualude.roadsign.dto.SolarEnergyFaultDto(solarEnergyId, solarType, solarOrgId, solarMainOrgId) from SolarFaultEntity where solarFaultId = ?1")
    public SolarEnergyFaultDto getInfoByFaultId(Integer solarFaultId) throws Exception;

    @Query(value = "select lf.solarCheck from SolarFaultEntity lf where lf.solarFaultId = ?1")
    public Integer getMachineCheck(Integer solarFaultId);

    @Modifying
    @Query(value = "UPDATE SolarFaultEntity  lf SET lf.solarCheck= ?1,lf.solarCheckerId=?4,lf.solarFeedback=?2,lf.solarCheckTime=?5,lf.solarMainCheck=?1,lf.solarMainCheckTime=?5,lf.solarMaintenanceStatus=1 WHERE lf.solarFaultId=?3")
    public Integer straightFaultAudit(Integer isPass, String feedback, Integer solarFaultId, Integer userId, String dateNowStr);
}
